package o;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.ActivityChooserView.ActivityChooserViewAdapter;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class rt {
    private final byte[] ˊ = new byte[256];
    private ByteBuffer ˋ;
    private int ˎ = 0;
    private rr ॱ;

    public rt ˎ(@NonNull ByteBuffer byteBuffer) {
        ˊ();
        this.ˋ = byteBuffer.asReadOnlyBuffer();
        this.ˋ.position(0);
        this.ˋ.order(ByteOrder.LITTLE_ENDIAN);
        return this;
    }

    public void ˎ() {
        this.ˋ = null;
        this.ॱ = null;
    }

    private void ˊ() {
        this.ˋ = null;
        Arrays.fill(this.ˊ, (byte) 0);
        this.ॱ = new rr();
        this.ˎ = 0;
    }

    @NonNull
    public rr ॱ() {
        if (this.ˋ == null) {
            throw new IllegalStateException("You must call setData() before parseHeader()");
        } else if (ˏॱ()) {
            return this.ॱ;
        } else {
            ᐝ();
            if (!ˏॱ()) {
                ˏ();
                if (this.ॱ.ˋ < 0) {
                    this.ॱ.ˊ = 1;
                }
            }
            return this.ॱ;
        }
    }

    private void ˏ() {
        ˎ((int) ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED);
    }

    private void ˎ(int i) {
        int i2 = 0;
        while (i2 == 0 && !ˏॱ() && this.ॱ.ˋ <= i) {
            switch (ˊॱ()) {
                case 33:
                    switch (ˊॱ()) {
                        case 1:
                            ˋॱ();
                            break;
                        case 249:
                            this.ॱ.ˏ = new rv();
                            ˋ();
                            break;
                        case 254:
                            ˋॱ();
                            break;
                        case 255:
                            ॱˊ();
                            StringBuilder stringBuilder = new StringBuilder();
                            for (int i3 = 0; i3 < 11; i3++) {
                                stringBuilder.append((char) this.ˊ[i3]);
                            }
                            if (!stringBuilder.toString().equals("NETSCAPE2.0")) {
                                ˋॱ();
                                break;
                            } else {
                                ʻ();
                                break;
                            }
                        default:
                            ˋॱ();
                            break;
                    }
                case 44:
                    if (this.ॱ.ˏ == null) {
                        this.ॱ.ˏ = new rv();
                    }
                    ʽ();
                    break;
                case 59:
                    i2 = 1;
                    break;
                default:
                    this.ॱ.ˊ = 1;
                    break;
            }
        }
    }

    private void ˋ() {
        boolean z = true;
        ˊॱ();
        int ˊॱ = ˊॱ();
        this.ॱ.ˏ.ʻ = (ˊॱ & 28) >> 2;
        if (this.ॱ.ˏ.ʻ == 0) {
            this.ॱ.ˏ.ʻ = 1;
        }
        rv rvVar = this.ॱ.ˏ;
        if ((ˊॱ & 1) == 0) {
            z = false;
        }
        rvVar.ʼ = z;
        int ͺ = ͺ();
        if (ͺ < 2) {
            ͺ = 10;
        }
        this.ॱ.ˏ.ᐝ = ͺ * 10;
        this.ॱ.ˏ.ॱॱ = ˊॱ();
        ˊॱ();
    }

    private void ʽ() {
        boolean z = true;
        this.ॱ.ˏ.ॱ = ͺ();
        this.ॱ.ˏ.ˎ = ͺ();
        this.ॱ.ˏ.ˏ = ͺ();
        this.ॱ.ˏ.ˋ = ͺ();
        int ˊॱ = ˊॱ();
        boolean z2 = (ˊॱ & DnsConfig.MAX_CACHE_ENTRIES) != 0;
        int pow = (int) Math.pow(2.0d, (double) ((ˊॱ & 7) + 1));
        rv rvVar = this.ॱ.ˏ;
        if ((ˊॱ & 64) == 0) {
            z = false;
        }
        rvVar.ˊ = z;
        if (z2) {
            this.ॱ.ˏ.ˋॱ = ˏ(pow);
        } else {
            this.ॱ.ˏ.ˋॱ = null;
        }
        this.ॱ.ˏ.ʽ = this.ˋ.position();
        ʼ();
        if (!ˏॱ()) {
            rr rrVar = this.ॱ;
            rrVar.ˋ++;
            this.ॱ.ˎ.add(this.ॱ.ˏ);
        }
    }

    private void ʻ() {
        do {
            ॱˊ();
            if (this.ˊ[0] == (byte) 1) {
                this.ॱ.ॱˊ = (this.ˊ[1] & 255) | ((this.ˊ[2] & 255) << 8);
            }
            if (this.ˎ <= 0) {
                return;
            }
        } while (!ˏॱ());
    }

    private void ᐝ() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            stringBuilder.append((char) ˊॱ());
        }
        if (stringBuilder.toString().startsWith("GIF")) {
            ॱॱ();
            if (this.ॱ.ʻ && !ˏॱ()) {
                this.ॱ.ॱ = ˏ(this.ॱ.ʼ);
                this.ॱ.ˋॱ = this.ॱ.ॱ[this.ॱ.ʽ];
                return;
            }
            return;
        }
        this.ॱ.ˊ = 1;
    }

    private void ॱॱ() {
        this.ॱ.ᐝ = ͺ();
        this.ॱ.ॱॱ = ͺ();
        int ˊॱ = ˊॱ();
        this.ॱ.ʻ = (ˊॱ & DnsConfig.MAX_CACHE_ENTRIES) != 0;
        this.ॱ.ʼ = (int) Math.pow(2.0d, (double) ((ˊॱ & 7) + 1));
        this.ॱ.ʽ = ˊॱ();
        this.ॱ.ͺ = ˊॱ();
    }

    @Nullable
    private int[] ˏ(int i) {
        int[] iArr;
        Throwable e;
        int i2 = 0;
        byte[] bArr = new byte[(i * 3)];
        try {
            this.ˋ.get(bArr);
            iArr = new int[256];
            int i3 = 0;
            while (i2 < i) {
                int i4 = i3 + 1;
                try {
                    int i5 = bArr[i3] & 255;
                    int i6 = i4 + 1;
                    int i7 = bArr[i4] & 255;
                    i3 = i6 + 1;
                    i4 = i2 + 1;
                    iArr[i2] = (((i5 << 16) | -16777216) | (i7 << 8)) | (bArr[i6] & 255);
                    i2 = i4;
                } catch (BufferUnderflowException e2) {
                    e = e2;
                }
            }
        } catch (Throwable e3) {
            Throwable th = e3;
            iArr = null;
            e = th;
            if (Log.isLoggable("GifHeaderParser", 3)) {
                Log.d("GifHeaderParser", "Format Error Reading Color Table", e);
            }
            this.ॱ.ˊ = 1;
            return iArr;
        }
        return iArr;
    }

    private void ʼ() {
        ˊॱ();
        ˋॱ();
    }

    private void ˋॱ() {
        int ˊॱ;
        do {
            ˊॱ = ˊॱ();
            this.ˋ.position(Math.min(this.ˋ.position() + ˊॱ, this.ˋ.limit()));
        } while (ˊॱ > 0);
    }

    private void ॱˊ() {
        int i = 0;
        this.ˎ = ˊॱ();
        if (this.ˎ > 0) {
            int i2 = 0;
            while (i2 < this.ˎ) {
                try {
                    i = this.ˎ - i2;
                    this.ˋ.get(this.ˊ, i2, i);
                    i2 += i;
                } catch (Throwable e) {
                    if (Log.isLoggable("GifHeaderParser", 3)) {
                        Log.d("GifHeaderParser", "Error Reading Block n: " + i2 + " count: " + i + " blockSize: " + this.ˎ, e);
                    }
                    this.ॱ.ˊ = 1;
                    return;
                }
            }
        }
    }

    private int ˊॱ() {
        int i = 0;
        try {
            return this.ˋ.get() & 255;
        } catch (Exception e) {
            this.ॱ.ˊ = 1;
            return i;
        }
    }

    private int ͺ() {
        return this.ˋ.getShort();
    }

    private boolean ˏॱ() {
        return this.ॱ.ˊ != 0;
    }
}
